package com.globalblue;

import java.net.URLConnection;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.globalblue.view.ImprovedMainWindow;

public class DownloadThreadHandler extends SwingWorker<Boolean, Integer> {
	private final Logger logger = Logger.getLogger(DownloadThreadHandler.class);
	private ImprovedMainWindow window;
	public static final String BASE_FOLDER = "C:/Delivery";
	private String ticketId;
	URLConnection urlConnection;

	public DownloadThreadHandler(ImprovedMainWindow window, String ticketId,
			URLConnection urlConnection) {
		this.window = window;
		this.ticketId = ticketId;
		this.urlConnection = urlConnection;
	}

	@Override
	protected Boolean doInBackground() throws Exception {
		FileDownloader fd = new FileDownloader();

		fd.downloadFile(urlConnection, Configurations.getZipFileLocation(),window);

		logger.log(Level.INFO, "Finish Downloading build file");

		logger.log(Level.INFO, "Extracting Files");
		window.changeStatus("images\\backup in progress.gif",
				"Extracting files...");
		FileUnzip unzip = new FileUnzip();
		unzip.ExtractAllFiles(Configurations.getZipFileLocation(),
				Configurations.getExtractedLocation());
		logger.log(Level.INFO, "Finished Extracting Files");

		FileListDisplay display = new FileListDisplay();
		List<String> fileList = display.getFileList(Configurations
				.getExtractedLocation());
		window.showFileListWindow(fileList);
		return true;

	}

	public void showStatus(String imagePath, String text) {
		JPanel newPanel = new JPanel();
		JLabel lblBackUpStatus = new JLabel(text);

		lblBackUpStatus.setIcon(new ImageIcon(imagePath));
		lblBackUpStatus.setBounds(272, 65, 339, 130);
		lblBackUpStatus.setVisible(true);
		newPanel.add(lblBackUpStatus);
		window.changeMainWindowContent(newPanel);
	}
}
